<div>
	<h3>Vyhledej v článcích</h3>
	<form action="" method="get">
		<label>Co: <input type="text" name="query"></label><br />
		<label>Kde: <select name="category[]" class="chzn-select" style="width:250px;" id="najdi" multiple="multiple">
			<option value="all" selected="selected">-ve všech kategoriích-</option>
			<?php 
					$kat = Category::getCategories();
					foreach ($kat as $value) {
						if (Category::hasParent($value["id"])) {
							continue;
						}
						echo '<option value="' . $value["id"] . '">' . $value["nazev"] . '</option>';
						if (Category::hasChildren($value["id"])) {
							echo '<optgroup label="' . $value["nazev"] . '">';
							foreach (Category::getChildren($value["id"]) as $value) {
								echo '<option value="' . $value["id"] . '">' . $value["nazev"] . '</option>';
							}
							echo '</optgroup>';
						}
					}
				?>
				</select></label><br />
				<label>Prohledat i podkategorie <input type="checkbox" name="podkategorie" value="1" checked="checked"></label><br />
				<label>Prohledat názvy: <input type="checkbox" name="nadpis" value="1" checked="checked"></label><br />
				<label>Prohledat anotaci: <input type="checkbox" name="anotace" value="1" checked="checked"></label><br />
				<label>Prohledat obsah: <input type="checkbox" name="obsah" value="1" checked="checked"></label><br />
				<input type="hidden" name="clanky">
				<input type="hidden" name="najdi_clanek">
				<input type="submit" value="Najdi článek">
	</form>
	<?php 
		if (isset($_GET["najdi_clanek"])) {
		echo "<h4>Výsledky hledání \"" . $_GET['query'];
		if($_GET['category'] == "all"){
			echo "\" ve všech kategoriích";
		}else{
			echo "\" pro kategorii \"" . Category::getName($_GET["category"]) . '"';
		}
		echo "</h4>";
		$clanky = Article::searchArticle($_GET["query"], $_GET["category"], $_GET["nadpis"], $_GET["anotace"], $_GET["obsah"], $_GET["podkategorie"]);
		
		if($clanky == false){
			echo "<h4>Bohužel, nebyly nalezeny žádné články</h4>";
		}else{
			foreach ($clanky as $value) {
				$co = $_GET["query"];
				$cim = "<span class=\"hledani\">$co</span>";
				if($_GET["nadpis"] == 1){
					$nazev = str_replace($co, $cim, $value["nazev"]);
				}else{
					$nazev = $value["nazev"];
				}
				if($_GET["anotace"] == 1){
					$anotace = str_replace($co, $cim, $value["anotace"]);
				}else{
					$anotace = $value["anotace"];
				}
				if($_GET["obsah"] == 1){
					$obsah = str_replace($co, $cim, $value["obsah"]);
				}else{
					$obsah = $value["obsah"];
				}
				echo "název: " . $nazev . "<br>";
				echo "anotace: " . $anotace . "<br>";
				echo "obsah: " . $obsah . "<br>";
				echo "autor: " . $value["autor"] . "<br>";
				echo "kategorie: ";
					foreach ($value["kategorie"] as $kat) {
						echo Category::getName($kat) . " ";
					}
				echo "<br>";
				echo "vytvoreno: " . $value["vytvoreno"] . "<br>";
				echo "zmena: " . $value["zmeneno"] . "<br>";
				echo "id: " . $value["id"] . "<hr>";
				}
			}
		}
	?>
</div>
<div>
	<h3>Nový článek</h3>
	<form action="" method="post">
		<label>Název: <input type="text" name="nazev"></label><br />
		<label>Kategorie: 
			<select name="kategorie[]" multiple="multiple" class="chzn-select" style="width:250px;" id="novy">
			<?php 
					$kat = Category::getCategories();
					foreach ($kat as $value) {
						if (Category::hasParent($value["id"])) {
							continue;
						}
						echo '<option value="' . $value["id"] . '">' . $value["nazev"] . '</option>';
						if (Category::hasChildren($value["id"])) {
							echo '<optgroup label="' . $value["nazev"] . '">';
							foreach (Category::getChildren($value["id"]) as $value) {
								echo '<option value="' . $value["id"] . '">' . $value["nazev"] . '</option>';
							}
							echo '</optgroup>';
						}
					}
				?>
				</select>
		</label><br />
		<label>Anotace: <textarea name="anotace" rows="3" cols="20"></textarea></label><br />
		<label>Obsah: <textarea name="obsah" rows="3" cols="20"></textarea></label><br />
		<label>Autor: <input type="text" name="autor"></label><br />
		<label>Publikovat: <input type="checkbox" name="publikovat" value="1" checked="checked"></label>
		<input type="hidden" name="pridat_clanek">
		<input type="submit" value="Přidat článek">
	</form>
</div>
<?php 
	if(isset($_GET["upravit_clanek_form"])){
		$clanek = Article::getArticle($_GET["id"]);
		?>
		<div>
			<h3>Upravit článek</h3>
			<form action="" method="post">
				<label>Název: <input type="text" name="nazev" value="<?php echo $clanek["nazev"]; ?>"></label><br />
				<label>Kategorie: 
				<select name="kategorie[]" multiple="multiple" class="chzn-select" style="width:250px;" id="uprava">
				<?php 
					$kat = Category::getCategories();
					foreach ($kat as $value) {
						echo '<option value="' . $value["id"] . '"';
						if(in_array($value["id"], $clanek["kategorie"])){
							echo ' selected="selected"';
						}
						echo '>' . $value["nazev"] . '</option>';
					}
				?>
					</select>
				</label><br />
				<label>Anotace: <textarea name="anotace" rows="3" cols="20"><?php echo $clanek["anotace"]; ?></textarea></label><br />
				<label>Obsah: <textarea name="obsah" rows="3" cols="20"><?php echo $clanek["obsah"]; ?></textarea></label><br />
				<input type="hidden" name="upravit_clanek">
				<input type="hidden" name="id" value="<?php echo $clanek["id"]; ?>">
				<input type="submit" value="Upravit článek">
			</form>
		</div>
		<?php 
	}
?>
<div>
	<h3>Články</h3>
	<?php
		$clanky = Article::getArticles();
		echo "<div>";
		Article::generateStrankovani();
		echo "</div>";
		foreach ($clanky as $value) {
			echo '<a href="?smazat_clanek&amp;id=' . $value["id"] . '">Smazat článek</a><br>';
			echo '<a href="?clanky&amp;upravit_clanek_form&amp;id=' . $value["id"] . '">Upravit článek</a><br>';
			echo "název: " . $value["nazev"] . "<br>";
			echo "anotace: " . $value["anotace"] . "<br>";
			echo "obsah: " . $value["obsah"] . "<br>";
			echo "autor: " . $value["autor"] . "<br>";
			echo "kategorie: ";
				foreach ($value["kategorie"] as $kat) {
					echo Category::getName($kat) . " ";
				}
			echo "<br>";
			echo "vytvoreno: " . $value["vytvoreno"] . "<br>";
			echo "zmena: " . $value["zmeneno"] . "<br>";
			echo "id: " . $value["id"] . "<hr>";
		}
		echo "<div>";
		Article::generateStrankovani();
		echo "</div>";
	?>
</div>
